package org.csu.jpetstore.service.impl;
import jakarta.annotation.Resource;
import org.csu.jpetstore.persistence.*;
import org.csu.jpetstore.domain.Order;
import org.csu.jpetstore.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Override
    public List<Order> getOrdersByUserID(String userID) {
        return orderMapper.getOrdersByUserID(userID);
    }

    @Override
    public Order getOrder(int orderId){
        return orderMapper.getOrder(orderId);
    }

    @Override
    @Transactional
    public void updateOrderStatusById(int ordersID, String username) {
        Order select = orderMapper.getOrder(ordersID);
        if(select==null){
            throw new IllegalStateException("OrderId不存在");
        }
        if (username!=null && !username.isEmpty()
                &&Objects.equals(username,select.getUserId())){
            orderMapper.updateOrderStatusById(ordersID);
        }
    }

    @Override
    public List<Order> getOrders() {
        return orderMapper.getOrderS();
    }

    @Override
    public void updateOrderById(int ordersID, Order order) {
        orderMapper.updateOrderById(ordersID,order);
    }

    @Override
    public void addOrder(Order order) {
        orderMapper.addOrder(order);
    }

}